#ifndef GATHER_DATA_9982_H_
#define GATHER_DATA_9982_H_

#include "Config.h"
#include <QVector>
#include <QAtomicInt>
#include <QSharedPointer>
#include <QMap>
#include <QFile>
#include <QDateTime>

struct GatherDataBuffer
{
public:
    GatherDataBuffer(int size);

    float sum;
    float *data();
    int size();

    bool attach();
    void detach();    
    
    QVector<float> buffer;
    QAtomicInt flag;
};
typedef QSharedPointer<GatherDataBuffer> GatherBufferPtr;

class GatherDataBufferManager
{
public:
    GatherDataBufferManager();

    void init(int size);
    int size();
    GatherDataBuffer *getBuffer();    

protected:
    GatherBufferPtr malloc(int size);
    QVector<GatherBufferPtr> m_buffers;
    int m_size;
};

struct GatherFile
{   
    struct FrameInfo{                
        qint64 offset;
        int size;
        float sum;
        QDateTime time;
    };

    GatherFile();

    void beignSave(QString filepath, Param_USB9982 gatherParam);
    void endSave();

    void load(QString filepath);    
    void close();
    bool isOpen();
    bool isSave();

    void append(QDateTime cur,float sum,const QByteArray &data);
    bool getFrame(int index,QDateTime &time, QVector<float> &data);
    void calcBuffer(const QByteArray &buffer, QVector<float> &data);

    Param_USB9982 param;    
    QVector<FrameInfo> frameInfo;    

    qint64 saveSize;
    qint64 maxSize;
    QFile file;
    bool bSave;

    QByteArray m_buffer;
    QByteArray m_zipBuffer;
};
static_assert(sizeof(Param_USB9982) + sizeof(GatherFile::FrameInfo) * 10000 < 1024 * 1024, "size is to big");




#endif

